package org.codert.jpa.web;

import lombok.Data;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author: TanJingyu
 * @create: 2021-08-19 16:28
 **/
@Controller
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/direct")
    public String direct() {
        return "index.html";
    }

//    @GetMapping("/{no:[0-9]+}")
//    @ResponseBody
//    public String test(@PathVariable String no) {
//        return no;
//    }

    @GetMapping("/time")
    @ResponseBody
    public String time(@Validated RequestData date) {
        System.out.println(date.toString());
        return date.toString();
    }

    @Data
    public static class RequestData {
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @NotNull
        private Date date;
    }

    @GetMapping
    public ResponseEntity<String> hello(String[] strings, Integer count) {
        System.out.println("业务逻辑,hello");
        return ResponseEntity.ok("hello测试.....");
    }

    /**
     * http://localhost:8080/hello/page?page=1000&size=1000&sort=name,desc&sort=age,asc
     * @param pageable
     * @param sort
     * @return
     */
    @GetMapping("/page")
    @ResponseBody
    public Object page(Pageable pageable, @RequestParam(name = "sort") List<String> sort) {
        System.out.println(pageable.getSort());
        System.out.println(sort);
        System.out.println(sort.toString().replace(" ", ""));
        return pageable;
    }
}
